import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument(f'user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36')
options.binary_location=r'/home/fan/APP/谷歌浏览器/chrome'
# from selenium.webdriver.chrome.service import Service
# driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', options=options)
# wd = webdriver.Chrome(service=Service(r'd:\tools\chromedriver.exe'))
# 创建 WebDriver 对象，指明使用chrome浏览器驱动
from selenium.webdriver.common.by import By
wd = webdriver.Chrome(options=options)
wd.implicitly_wait(5)


data = ''
# 调用WebDriver 对象的get方法 可以让浏览器打开指定网址
def readhtml(url:str):
    global data
    wd.get(url)
    data = wd.execute_script('return jsarray;')
def findid(idx:str):
    # wd.implicitly_wait(5)
    element = wd.find_element(By.ID, idx)
    return element
index = 0
def jx():
    global index ,data
    tm = findid("tm")
    a = findid("a")
    b = findid("b")
    c = findid("c")
    d = findid("d")
    ok = wd.find_element(By.CSS_SELECTOR,'.tt_2[name=cmdjj]')
    k1next = findid("k1next")
    for j in data:
        if j is not None:
            index += 1
            print(j[0],"\t--->",j[-1])
            if int(j[-1]) == 1:
                a.click()
                pass
            elif int(j[-1]) == 2:
                b.click()
                pass
            elif int(j[-1]) == 3:
                c.click()
                pass
            elif int(j[-1]) == 4:
                d.click()
                pass
            if index == 100:
                ok.click()
            else:
                k1next.click()
if __name__ == '__main__':
    readhtml("http://www.jiakao.com/cnty/web/km1.php?mcx=1")
    wd.implicitly_wait(5)
    try:
        jx()
    except BaseException as err:
        print(err)
    finally:
        time.sleep(50)
        wd.quit()  # 关掉driver